package com.phuoc.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQLiteHelper extends SQLiteOpenHelper {

	// Table Establishment
	public static final String TABLE_ESTABLISMENT = "ESTABLISHMENT";
	public static final String ESTAB_ID = "ID";
	public static final String ESTAB_NAME = "NAME";
	public static final String ESTAB_TYPEOFESTAB = "IDTYPEOFESTAB";
	public static final String ESTAB_TYPEOFFOOD = "IDTYPEOFFOOD";
	public static final String ESTAB_LOCATION = "LOCATION";
	public static final String ESTAB_DATEREVIEW = "DATEREVIEW";
	public static final String ESTAB_RATEASMOSPHERE = "RATEASMOSPHERE";
	public static final String ESTAB_RATESERVICE = "RATESERVICE";
	public static final String ESTAB_RATEFOOD = "RATEFOOD";
	public static final String ESTAB_RATEOVERALL = "RATEOVERALL";
	public static final String ESTAB_TYPEOFMEAL = "TYPEOFMEAL";
	public static final String ESTAB_COST = "COST";
	public static final String ESTAB_COMMENT = "COMMENT";

	// Table Type of Estab
	public static final String TABLE_TYPE_OF_ESTABLISHMENT = "TYPE_OF_ESTABLISHMENT";
	public static final String TYPE_ESTAB_ID = "ID";
	public static final String TYPE_ESTAB_NAME = "NAME";

	// Table Type of Food
	public static final String TABLE_TYPE_OF_FOOD = "TYPE_OF_FOOD";
	public static final String TYPE_FOOD_ID = "ID";
	public static final String TYPE_FOOD_NAME = "NAME";

	// Database
	private static final String DATABASE_NAME = "MREVIEWER.DB";
	private static final int DATABASE_VERSION = 1;

	// SQL establishment creation
	private static final String ESTABLISMENT_CREATION = "CREATE TABLE "
			+ TABLE_ESTABLISMENT + " (" + ESTAB_ID + " TEXT PRIMARY KEY, "
			+ ESTAB_NAME + " TEXT NOT NULL, " + ESTAB_TYPEOFESTAB
			+ " INTEGER, " + ESTAB_TYPEOFFOOD + " INTEGER," + ESTAB_LOCATION
			+ " TEXT, " + ESTAB_DATEREVIEW + " DATE, " + ESTAB_RATESERVICE
			+ " FLOAT, " + ESTAB_RATEASMOSPHERE + " FLOAT, " + ESTAB_RATEFOOD
			+ " FLOAT, " + ESTAB_RATEOVERALL + " FLOAT," + ESTAB_TYPEOFMEAL
			+ " TEXT, " + ESTAB_COST + " FLOAT," + ESTAB_COMMENT + " TEXT)";

	// SQL type of estab
	private static final String TYPE_ESTAB_CREATION = "CREATE TABLE "
			+ TABLE_TYPE_OF_ESTABLISHMENT + " (" + TYPE_ESTAB_ID
			+ " INTEGER PRIMARY KEY autoincrement, " + TYPE_ESTAB_NAME
			+ " TEXT NOT NULL)";

	// SQL type of food
	private static final String TYPE_FOOD_CREATION = "CREATE TABLE "
			+ TABLE_TYPE_OF_FOOD + " (" + TYPE_FOOD_ID
			+ " INTEGER PRIMARY KEY autoincrement, " + TYPE_FOOD_NAME
			+ " TEXT NOT NULL)";

	// Constructor
	public MySQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// Create database
		db.execSQL(ESTABLISMENT_CREATION);
		db.execSQL(TYPE_ESTAB_CREATION);
		db.execSQL(TYPE_FOOD_CREATION);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_ESTABLISMENT);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_TYPE_OF_ESTABLISHMENT);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_TYPE_OF_FOOD);
		onCreate(db);
	}

}
